package com.jboltai.capability.vdb.types.tencent;

import com.alibaba.fastjson.JSONObject;
import com.jboltai.resource.embedding.EmbeddingModel;
import com.jboltai.resource.vdb.VDBResource;
import com.jboltai.resource.vdb.VDBType;
import com.tencent.tcvectordb.client.VectorDBClient;
import com.tencent.tcvectordb.model.param.database.ConnectParam;
import com.tencent.tcvectordb.model.param.enums.ReadConsistencyEnum;

public class TencentVDBResource extends VDBResource {

    private VectorDBClient client;
    /**
     * 连接超时时间
     */
    private int connectTimeout = 5;



    /**
     * 资源
     * @param id 资源id
     * @param url
     * @param username
     * @param password
     */
    public TencentVDBResource(String id, String url, String username, String password, EmbeddingModel embeddingModel) {
        super(id, VDBType.TENCENT, url, username, password,embeddingModel);
    }
    /**
     * 资源
     * @param id 资源id
     * @param url
     * @param username
     * @param password
     */
    public TencentVDBResource(String id, String url, String username, String password, EmbeddingModel embeddingModel, JSONObject config) {
        super(id, VDBType.TENCENT, url, username, password,embeddingModel);
        if (config != null && config.containsKey("connectTimeout")) {
            this.connectTimeout = config.getInteger("connectTimeout");
        }

    }



    public Integer getConnectTimeout() {
        return connectTimeout;
    }

    public TencentVDBResource setConnectTimeout(int connectTimeout) {
        this.connectTimeout = connectTimeout;
        return this;
    }


    public VectorDBClient getClient() {
        if (client != null) {
            return client;
        }
        synchronized (this) {
            if (client != null) {
                return client;
            }
            try {
                client = new VectorDBClient(ConnectParam.newBuilder()
                        .withUrl(url)
                        .withUsername(username)
                        .withKey(password)
                        .withTimeout(connectTimeout)
                        .build(), ReadConsistencyEnum.EVENTUAL_CONSISTENCY);

            } catch (Exception e) {
                throw new RuntimeException("腾讯向量数据库client创建失败", e);
            }
        }
        return client;
    }


}
